//
//  UpdateNameViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/2.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "UpdateNameViewController.h"

@interface UpdateNameViewController ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UITextField * nameTextFiled;

@end

@implementation UpdateNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"修改昵称";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIButton * backButton1 = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0 , 0 , 65 * SCREENH6, 20 * SCREENH6);
    [backButton1 setTitle:@"保存" forState:UIControlStateNormal];
    backButton1.titleLabel.font = [UIFont boldSystemFontOfSize:13 * SCREENH6];
    [backButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [backButton1 addTarget:self action:@selector(clickUpdateNameButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xf1f1f1);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = [UIColor whiteColor];
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.text = @"昵称";
    nameLabel.textColor = ColorWithRGB(0xb2b2b2);
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.nameLabel = nameLabel;
    
    [self.backView addSubview:self.nameLabel];
    
    
    UITextField * nameTextFiled = [[UITextField alloc] init];
    
    nameTextFiled.borderStyle = UITextBorderStyleNone;
    nameTextFiled.backgroundColor = [UIColor clearColor];
    //设置光标颜色
    nameTextFiled.tintColor = [UIColor blackColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xb2b2b2);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入您的昵称" attributes:attrs];
    nameTextFiled.textColor = [UIColor blackColor];
    nameTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];

    //    accountText.keyboardType = UIKeyboardTypeNumberPad;
    //    accountText.keyboardAppearance = UIKeyboardAppearanceDark;
    self.nameTextFiled = nameTextFiled;
    
    [self.backView addSubview:self.nameTextFiled];
}


-(void)viewDidLayoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
    }];
    
    [self.nameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(55 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.centerY.mas_equalTo(self.backView);
    }];
}


- (void)clickUpdateNameButton{
    
    [[ZSNetworkTool sharedTool] submitChangePersonInfoWithUsername:self.nameTextFiled.text andBirthday:nil sex:nil resultWithBlock:^(BOOL success, NSDictionary * responseData) {
        
//        NSLog(@"responseData===%@",responseData);
        
        NSString * status= [NSString stringWithFormat:@"%@",responseData[@"status"]];
        
        if ([status isEqualToString:@"200"]) {
            
            __weak typeof(self) weakself = self;
            
            if (weakself.returnValueBlock) {
                //将自己的值传出去，完成传值
                weakself.returnValueBlock(self.nameLabel.text);
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
        
    }];
    
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
